<template>
  <div class="login">
    <div class="login_box">
      <!-- 头像部分 -->
      <div class="login_pic">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单部分 -->
      <el-form
        ref="loginFormref"
        :rules="loginRules"
        :model="loginData"
        label-width="0px"
        class="login_from"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginData.username"
            prefix-icon="iconfont icon-users"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginData.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginData: {
        username: "admin",
        password: "123456",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入登录用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      this.$refs.loginFormref.resetFields();
    },
    login() {
      this.$refs.loginFormref.validate((value) => {
        if (value) {
          this.$http
            .post("http://127.0.0.1:8888/api/private/v1/login", this.loginData)
            .then((response) => {
              if (response.data.meta.status !== 200) {
                this.$message.error("登录失败");
              } else {
                this.$message.success("登录成功");
                window.sessionStorage.setItem(
                  "token",
                  response.data.data.token
                );
                this.$router.push("/home");
              }
            })
            .catch((error) => console.log(error));
        }
      });
    },
  },
};
</script>

<style scoped>
.login {
  position: relative;

  background-color: rgb(51, 29, 65);
  height: 100%;
}
.login_box {
  width: 560px;
  height: 350px;
  background-color: rgb(255, 255, 255, 0.8);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.login_pic {
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, -50%);
  width: 100px;
  height: 100px;
  border: 1px solid #ddd;
  border-radius: 100%;
  padding: 10px;
  box-shadow: 0 0 10px;
}
img {
  width: 100%;
  height: 100%;
  border-radius: 100%;
}
.login_from {
  position: absolute;
  bottom: 25px;
  width: 100%;
  padding: 0 15px;
  box-sizing: border-box;
}
.btn {
  display: flex;
  justify-content: center;
}
</style>